package com.zs;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

/**
 * Description:
 *  使用  fanout Exchange
 * @author: zsq-1186
 * Date: 2017-08-16-17:10
 */
public class LogFanout {
    //队列名称
    private final static String EXCHANGE_NAME = "log";

    public static void main(String[] args) throws IOException, TimeoutException {
        //实例化工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置相关参数,地址，端口，账号，密码
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        //获取connection
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();
        //fanout类型转发器特别简单，把所有它接收到的消息，广播到所有它所知道的队列
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        String message=new Date().toLocaleString()+" -- log .....message...";

        channel.basicPublish(EXCHANGE_NAME,"",null,message.getBytes());



        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }
}
